package com.example.codegen;

import com.example.utils.CJUtils;

import java.util.Optional;

public class DeclareExpression implements Expression {
    public Optional<Type> variableType;
    public Variables variable;

    public DeclareExpression(Optional<Type> type, Variables variables) {
        this.variableType = type;
        this.variable = variables;
    }

    public String toJava() {
        return (variableType.map(type -> type.toJava() + " ").orElse("")) + variable.toJava();
    }

    public String toGo() {
        if (variableType.isPresent()) {
            return "var " + variable.toGo() + " " + variableType.get().toGo();
        } else {
            return variable.toGo();
        }
    }

    @Override
    public String toJs() {
        return "var " + variable.toJs();
    }

    @Override
    public String toCSharp() {
        return (variableType.map(type -> type.toCSharp() + " ").orElse("")) + variable.toCSharp();
    }

    @Override
    public String toCJ() {
        String name = variable.toCJ();
        if(variableType.isPresent()){
            if(CJUtils.spawnParamSet.contains(name))
                return "let " + name + ": " + variableType.get().toCJ();
            else
                return "var " + name + ": " + variableType.get().toCJ();
        }else {
            return name;
        }
    }
}
